/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SchedulingScreen.java
 *
 * Created on 09/03/2011, 13:07:11
 */
package wf.ui.cmps;

import bc.swing.pfrm.BaseParamModel;
import bc.swing.pfrm.FieldParamModel.ChangeListener;
import bc.swing.pfrm.Model;
import bc.swing.pfrm.Page;
import bc.swing.pfrm.PageLayout;
import bc.swing.pfrm.ParamView;
import java.util.Calendar;

/**
 *
 * @author bennyl
 */
public class CalendarView extends javax.swing.JPanel implements PageLayout, ParamView {

    Page page;
    private static final String[] monthNames = new String[]{
        "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
    };
    int month;
    int year;
    CalendarDaysView cdv = null;

    /** Creates new form SchedulingScreen */
    public CalendarView() {
        initComponents();
        Calendar cdr = Calendar.getInstance();
        setDate(cdr.get(Calendar.MONTH) + 1, cdr.get(Calendar.YEAR));
    }

    private void setMonth(int month){
        this.month = month;
    }
    
    public void setDate(int month, int year) {
        setMonth(month);
        this.year = year;
        this.monthLabel.setText("" + monthNames[month - 1] + " " + year);
        
        if (cdv != null) {
            this.contentPan.remove(cdv);
        }
        
        int day = (cdv == null? 1: cdv.getSelected());
        cdv = new CalendarDaysView(day, month, year);
        attachListener(cdv);
        this.contentPan.add(cdv);
        validate();
        repaint();
    }

    private void attachListener(CalendarDaysView cdv) {
        cdv.setListener(new CalendarDaysView.Listener() {

            @Override
            public void selectionChanged(int selection) {
                Calendar cd = Calendar.getInstance();
                cd.set(year, month-1, selection);
                page.getParam(SchedulingModel.SELECTED_DATE_PARAM).setValue(cd);
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        contentPan = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        jXHyperlink1 = new org.jdesktop.swingx.JXHyperlink();
        monthLabel = new javax.swing.JLabel();
        jXHyperlink2 = new org.jdesktop.swingx.JXHyperlink();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel10 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        contentPan.setMinimumSize(new java.awt.Dimension(100, 52));
        contentPan.setPreferredSize(new java.awt.Dimension(400, 100));
        contentPan.setLayout(new java.awt.BorderLayout());

        jPanel12.setPreferredSize(new java.awt.Dimension(100, 45));
        jPanel12.setLayout(new java.awt.GridBagLayout());

        jPanel13.setBackground(new java.awt.Color(153, 153, 153));

        jXHyperlink1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/calendar-prev16.png"))); // NOI18N
        jXHyperlink1.setText("");
        jXHyperlink1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink1ActionPerformed(evt);
            }
        });
        jPanel13.add(jXHyperlink1);

        monthLabel.setFont(new java.awt.Font("MS Reference Sans Serif", 1, 11)); // NOI18N
        monthLabel.setForeground(new java.awt.Color(255, 255, 255));
        monthLabel.setText("April 2011");
        jPanel13.add(monthLabel);

        jXHyperlink2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/calendar-next16.png"))); // NOI18N
        jXHyperlink2.setText("");
        jXHyperlink2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink2ActionPerformed(evt);
            }
        });
        jPanel13.add(jXHyperlink2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        jPanel12.add(jPanel13, gridBagConstraints);

        jPanel3.setBackground(new java.awt.Color(153, 153, 153));
        jPanel3.setMinimumSize(new java.awt.Dimension(550000000, 20));
        jPanel3.setPreferredSize(new java.awt.Dimension(10000000, 20));
        jPanel3.setLayout(new java.awt.GridLayout(1, 7, 3, 3));

        jPanel4.setBackground(new java.awt.Color(153, 153, 153));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Sunday");
        jPanel4.add(jLabel1);

        jPanel3.add(jPanel4);

        jPanel5.setBackground(new java.awt.Color(153, 153, 153));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Monday");
        jPanel5.add(jLabel2);

        jPanel3.add(jPanel5);

        jPanel6.setBackground(new java.awt.Color(153, 153, 153));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Tuesday");
        jPanel6.add(jLabel3);

        jPanel3.add(jPanel6);

        jPanel7.setBackground(new java.awt.Color(153, 153, 153));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Wednesday");
        jPanel7.add(jLabel4);

        jPanel3.add(jPanel7);

        jPanel8.setBackground(new java.awt.Color(153, 153, 153));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Thursday");
        jPanel8.add(jLabel5);

        jPanel3.add(jPanel8);

        jPanel9.setBackground(new java.awt.Color(153, 153, 153));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Friday");
        jPanel9.add(jLabel6);

        jPanel3.add(jPanel9);

        jPanel10.setBackground(new java.awt.Color(153, 153, 153));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Saturday");
        jPanel10.add(jLabel7);

        jPanel3.add(jPanel10);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.weightx = 1.0;
        jPanel12.add(jPanel3, gridBagConstraints);

        contentPan.add(jPanel12, java.awt.BorderLayout.PAGE_START);

        add(contentPan, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jXHyperlink1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink1ActionPerformed
        int m = month;
        int y = year;

        if (month == 1) {
            m = 12;
            y = year - 1;
        } else {
            m--;
        }
        updatePageDate(m, y);
        setDate(m, y);
    }

    private void updatePageDate(int m, int y) {
        Calendar cl = (Calendar) page.getParam(SchedulingModel.SELECTED_DATE_PARAM).getValue();
//        cl.set(Calendar.DAY_OF_MONTH, 1);
        cl.set(Calendar.MONTH, m - 1);
        cl.set(Calendar.YEAR, y);
        page.getParam(SchedulingModel.SELECTED_DATE_PARAM).setValue(cl);
    }//GEN-LAST:event_jXHyperlink1ActionPerformed

    private void jXHyperlink2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink2ActionPerformed
        int m = month;
        int y = year;

        if (month == 12) {
            m = 1;
            y = year + 1;
        } else {
            m++;
        }

        updatePageDate(m, y);
        setDate(m, y);
    }//GEN-LAST:event_jXHyperlink2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel contentPan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink1;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink2;
    private javax.swing.JLabel monthLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setPage(Page page) {
        this.page = page;
        updateDate();
    }

    private void updateDate() {
        Calendar d = (Calendar) page.getParam(SchedulingModel.SELECTED_DATE_PARAM).getValue();
        year = d.get(Calendar.YEAR);
        month = d.get(Calendar.MONTH) + 1;
        setDate(month, year);
    }

    @Override
    public void onDispose() {
    }

    @Override
    public void setParam(BaseParamModel param) {
        setPage(Page.get((Model) (param.getValue())));
    }

    @Override
    public void reflectChangesToParam(BaseParamModel to) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void onChange(BaseParamModel source, Object newValue, Object deltaHint) {
        updateDate();
    }
}
